package com.example.service.impl;

import com.example.dao.MovieDao;
import com.example.entity.Movie;
import com.example.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieDao movieDao;

    @Override
    public List<Movie> getAllMovies() {
        return movieDao.findAllMovies();
    }

    @Override
    public Movie getMovieById(Integer id) {
        return movieDao.findMovieById(id);
    }

    @Override
    @Transactional
    public boolean addMovie(Movie movie) {
        if (movie == null) {
            return false;
        }

        // 设置创建时间和更新时间
        Date now = new Date();
        movie.setCreateTime(now);
        movie.setUpdateTime(now);

        return movieDao.addMovie(movie) > 0;
    }

    @Override
    @Transactional
    public boolean updateMovie(Movie movie) {
        if (movie == null || movie.getId() == null) {
            return false;
        }

        // 设置更新时间
        movie.setUpdateTime(new Date());

        return movieDao.updateMovie(movie) > 0;
    }

    @Override
    @Transactional
    public boolean deleteMovie(Integer id) {
        if (id == null) {
            return false;
        }

        return movieDao.deleteMovie(id) > 0;
    }

    @Override
    public List<Movie> getMoviesByType(String types) {
        return movieDao.findMoviesByType(types);
    }
}
